<?php
/**
 * Created by PhpStorm.
 * User: 峰
 * Date: 2018/12/14
 * Time: 11:29
 */


namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Validate;

class User extends Common
{
    //分页条数
    private $page = 10;

    //主页显示
    public function index(){
        //查询
        $data['list'] = Db::table('xt_user')
            ->field('id,username,password,create_time,update_time')
            ->where('delete_time',null)
            ->paginate($this->page,false);
        return view("index",$data);
    }

    //添加页面
    public function show_add(){
        return view('add');
    }
    //编辑页面
    public function show_edit(){
        $data['info'] = Db::table('xt_user')
            ->where('id','=',input('get.id'))
            ->find();
        return view('edit',$data);

    }

    //添加
    public function add(){
        $add = Db::table('xt_user')
            ->insert([
            'username'  => input('post.name'),
            'password'  => md5(md5(input('post.pwd'))),
            'create_time'  => time(),
            'update_time'  => time(),
            ]);

        if (!$add){
            $this->error('添加失败','index');
        }else{
            $this->success('添加成功','index');
        }
    }

    //编辑
    public function edit(){
        $edit = Db::table('xt_user')
            ->where('id','=',input('post.id'))
            ->update([
                'username'  => input('post.name'),
                'password'  => md5(md5(input('post.pwd'))),
                'update_time' => time(),
            ]);
        if (!$edit){
            $this->error('更新失败','index');
        }else{
            $this->success('更新成功','index');
        }
    }

    //删除
    public function del(){
        $del = Db::table('xt_user')
            ->where('id', '=' , input('get.id'))
            ->update([
                'delete_time'   => time(),
            ]);
        if(!$del){
            return retJson('', 201, '删除失败！');
        }
        return retJson('', 200, '删除成功！');
    }
}